import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path"
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { viteMockServe } from 'vite-plugin-mock'

// https://vite.dev/config/
export default defineConfig(({command,mode}) => {
    const env = loadEnv(mode, process.cwd())
    return {
        plugins: [vue(),
            viteMockServe({
                enable: command === 'serve',
                mockPath: 'mock', // 指定mock目录
                //@ts-ignore
                localEnabled: command === 'serve', // 兼容旧版本配置
                watchFiles: true, // 监视mock目录下文件变化
            }),
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
                symbolId: 'icon-[dir]-[name]',
            }),
        ],
        resolve: {
            alias: {
                "@": path.resolve("./src") // 相对路径别名配置，使用 @ 代替 src
            }
        },
        css: {/*配置scss变量全局可用*/
            preprocessorOptions: {
                scss: {
                    // @ts-ignore
                    javascriptEnabled: true,
                    additionalData: '@use "@/styles/variable.scss" as *;\n@use "@/styles/mixins.scss" as *;',
                },
            },
        },
        // 添加代理配置以支持 mock
        server: {
            proxy: {
                [env.VITE_APP_BASE_API]: {
                    //获取数据服务器地址的设置
                    target: env.VITE_APP_BASE_URL,
                    //需要代理跨域
                    changeOrigin: true,
                    //路径重写
                    rewrite: (path) => path.replace(/^\/api/, ''),
                },
            },
        },
    }
})
